package com.hs.service;

import com.hs.config.PageObj;
import com.hs.dao.ClassLessonMapper;
import com.hs.dao.StudentMapper;
import com.hs.dao.YxClassMapper;
import com.hs.entity.ClassLesson;
import com.hs.entity.Student;
import com.hs.entity.YxClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class YxClassService {
    @Autowired
    YxClassMapper classMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ClassLessonMapper classLessonMapper;
    @Transactional
    public int insertSelective(YxClass record){
        record.setCreateTime(new Date());
        int i = classMapper.insertSelective(record);
        if(i == 0){
            throw new RuntimeException("用户插入失败");
        }
        //初始化用户的物品持有列表
        log.info("用户"+record.getName()+"初始化了"+(i - 1)+"物品持有列表");
        return  i;
    }
    public int updateByPrimaryKeySelective(YxClass record){
        return classMapper.updateByPrimaryKeySelective(record);
    };
    public List<YxClass> selectByCondition(YxClass record){
        return classMapper.selectByCondition(record);
    };
    public PageObj page(YxClass item){
        int total = classMapper.selectByConditionCount(item);
        if(total == 0){
            return new PageObj(item.getCurrentPage(), item.getPageSize(),total,null);
        }
        List<YxClass> list = classMapper.selectByCondition(item);
        for(YxClass i :list){
            Student student = new Student();
            student.setClassId(i.getId());
            int count = studentMapper.selectByConditionCount(student);
            i.setStudentNum(count);

            ClassLesson classLesson = new ClassLesson();
            classLesson.setClassId(i.getId());
            int countLesson = classLessonMapper.selectByConditionCount(classLesson);
            i.setLessonNum(countLesson);

        }
        return new PageObj(item.getCurrentPage(), item.getPageSize(),total,list);
    }
    public int selectByConditionCount(YxClass record){
        int count =  classMapper.selectByConditionCount(record);
        return count;
    }


}
